﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using H2.NET.Common;
using H2.NET.Data.Common;
using H2.NET.DBUtility;
using H2.NET.Web.Pages.SMD.Model;
using H2.NET.Web.Workflow.DwzCode;

namespace H2.NET.Web.Pages.SMD.InStock
{
    public partial class Logs : BasePage
    {
        public string rel = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            rel = AksRequest.GetString("category");

            if (!IsPostBack)
            {
                var strWhere = "1=1";
                if (rel == "kc")
                {
                    strWhere += " and Category = '库存变更'";
                }
                else if (rel == "ys")
                {
                    strWhere += " and Category = '设备预算'";
                }

                TotalItemCount = (t_smd_InStockLog._ID > 0 
                    & t_smd_InStockLog._Category == (rel == "kc" ? "库存变更" : "设备预算")).Count<t_smd_InStockLog>(Storage.Context3);
                PageNumber = Convert.ToInt32(Request.Form["pageNum"] ?? "1");

                rpt_logs.DataSource = GetListByPage(strWhere, "", PageSize*(PageNumber - 1) + 1, PageSize*PageNumber);
                rpt_logs.DataBind();
            }
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from t_smd_InStockLog T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);

            DbHelperSQL.connectionString = PubConstant.GetConnectionString("smdDB");
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}